import http from '../../utils/http'

import { loadingFun } from '../../utils/tools'

Page({

	/**
	 * 页面的初始数据
	 */
	data: {

        // 新品推荐
        hotGoods: [],
        // 轮播图
        imgUrls: [],

        // 商品列表
        goodsList: [],
        status: 'loading',
        queryParams: {
            status: 1,
            page: 1,
            limit: 1015
        },

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
        this.getGoodsListFun()
        this.getIndexImages()
	},

    /**
     * 请求数据
     * @param {*} queryParams 
     * @returns 
     */
    async getGoodsList(queryParams) {
        return await http.get('/api/mall/pmsspu/page', queryParams)
    },

    /**
     * 分页加载数据
     * @returns 
     */
    async getGoodsListFun() {
        let { goodsList, queryParams, status } = this.data

        // 分页加载数据
        const data = await loadingFun(this.getGoodsList, queryParams, goodsList, status)
        if (!data) return

        this.setData({ goodsList: data.dataList, queryParams: data.queryParams, status: data.status })
    },

    async getIndexImages() {
        let imgs = await http.get('/api/mall/advertise/showAdvertise/index')
        let urls = []
        if(imgs.status) {
            imgs.data.advertiseItem.forEach((item, index) => {
                urls.push(item.pic)
            })
            this.setData({ imgUrls: urls })
        } else {
            this.setData({ imgUrls: [] })
        }
    },

    // async getGoodsList() {

    //     let baseGoods = await http.get('http://likeshopv2.yixiangonline.com/api/goods/getBestList?page_no=1')
    //     this.setData({ baseGoods: baseGoods.data.list })

    //     let hotGoods = await http.get('http://likeshopv2.yixiangonline.com/api/index/lists')
    //     this.setData({ hotGoods: hotGoods.data.host_goods })


    //     let imgs = await http.get('http://likeshopv2.yixiangonline.com/api/goods/getGoodsDetail?id=10')
    //     let urls = []
    //     imgs.data.goods_image.forEach((item, index) => {
    //         urls.push(item.uri)
    //     })
    //     this.setData({ imgUrls: urls })
    // },


	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
        wx.stopPullDownRefresh()
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
        let { status } = this.data
        if(status !== 'finished') {
            this.getGoodsListFun()
        }
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})